#ifndef WEBCLIENT_HPP
#define WEBCLIENT_HPP

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/websocket.hpp>
#include <string>
#include <vector>

namespace beast = boost::beast;
namespace http = beast::http;
namespace websocket = beast::websocket;
namespace net = boost::asio;
namespace ssl = net::ssl;
using tcp = net::ip::tcp;

class WebClient {
private:
    std::string host;
    std::string port;
    std::string path;
    std::string url;
    net::io_context ioc;
    ssl::context ctx;
    tcp::resolver resolver;
    bool isSecure;
    union {
        websocket::stream<tcp::socket> ws;
        websocket::stream<beast::ssl_stream<tcp::socket>> wss;
    };
    bool connected = false;

    // 解析 URL 的私有方法
    void parseUrl(const std::string& url);

public:
    WebClient();
    WebClient(std::string host, std::string port, std::string path, bool isSecure = false);
    WebClient(std::string url);
    ~WebClient();

    void config(std::string host, std::string port, std::string path, bool isSecure = false);
    void config(std::string url);
    int connect();
    int send(const std::string& message);
    int receive(std::string& received);
    void close();
};

#endif /* WEBCLIENT_HPP */